package com.yanqu.road.server.cmd.chefduel.process2;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.chefduel.ChefDuelProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.chefduel.ChefDuelActivityMgr;
import com.yanqu.road.server.manger.activity.chefduel.ChefDuelMgr;
import com.yanqu.road.server.manger.activity.chefduel.OtherActivityChefDuel2;
import com.yanqu.road.server.manger.activity.chefduel.OtherActivityChefDuelActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;

@Cmd(code = Protocol.S_CHEF_DUEL2_GET_TABLE_USER, desc = "获取大场景有资格参赛的玩家的座位列表")
public class ChefDuel2GetCookerCmd extends AbstractCommand {

	int activityId;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		ChefDuelProto.ChefDuel2CookerReqMsg reqMsg = ChefDuelProto.ChefDuel2CookerReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo activityInfo = ChefDuelActivityMgr.getOpenActivityInfo(activityId);
		if(activityInfo == null){
			ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
			return;
		}
		ret = ChefDuelMgr.sendCookerMsg(activityId,player.getUserId());
		if(ret == 0){
			//同时拉取赴宴范围的桌子
			for (Map.Entry<Integer, OtherActivityChefDuel2> entry : OtherActivityChefDuelActivityMgr.getChefDuel2Map().entrySet()) {
				if(ActivityMgr.activityInTime(entry.getValue().getActivityInfo())){
					entry.getValue().sendCookerMsg(packet.getPlayerId());
				}
			}
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		if(ret != 0){
			ChefDuelProto.ChefDuel2CookerRespMsg.Builder respMsg = ChefDuelProto.ChefDuel2CookerRespMsg.newBuilder();
			respMsg.setRet(ret);
			player.sendPacket(Protocol.U_CHEF_DUEL2_GET_TABLE_USER, respMsg);
		}
	}
}
